﻿using System;
using Tao.OpenGl;

namespace Aeon.Framework.Objects
{

/// <summary>
/// Triangle primitive class.
/// Uses up to 1 color and texture, for front face.
/// 
/// </summary>
public class Triangle : ObjectBase
    {
    // --- Attributtes:

    private Single height;      // Triangle half height.
    private Single lenght;      // Triangle half base length.
    
    // --- Constructor:

    /// <summary>
    /// Equilateral triangle constructor.
    /// 
    /// </summary>
    /// <param name="key"> Unique identifier. </param>
    /// <param name="origin"> Triangle orthocenter.  </param>
    /// <param name="angle"> Object orientation. </param>
    /// <param name="size"> Equilateral triangle side length. </param>
    public Triangle(String key, Position origin, Orientation angle, Single size)
        : this(key,origin,angle,size,(size*((3^(1/2))/3)),AeonMaterials.Default) { }
        
    /// <summary>
    /// Custom isoceles triangle constructor.
    /// 
    /// </summary>
    /// <param name="key"> Unique identifier. </param>
    /// <param name="origin"> Triangle orthocenter.  </param>
    /// <param name="angle"> Object orientation. </param>
    /// <param name="size"> Isoceles triangle base width. </param>
    /// <param name="height"> Isoceles triangle height. </param>
    /// <param name="material"> Object material. </param>
    public Triangle(String key, Position origin, Orientation angle, Single size, Single height, AeonMaterial material)
        : base(key,origin,angle,material)
        {
        this.height = height/2;
        lenght = size/2;
        }
        
    // --- Public Methods:

    public override void Draw()
        {       
        Gl.glTranslatef(origin.xPos,origin.yPos,origin.zPos);                    
        Gl.glRotatef(angle.xAng,1,0,0);
        Gl.glRotatef(angle.yAng,0,1,0);
        Gl.glRotatef(angle.zAng,0,0,1);
        
        Gl.glColor4f(material.Color.R, material.Color.G, material.Color.B, material.Color.A);
        material.Texture.Bind();
        
        Gl.glBegin(Gl.GL_TRIANGLES);
        Gl.glNormal3i(0,0,-1);
        
        Gl.glTexCoord2f(0.5f, 1.0f);
        Gl.glVertex2f(0, height);
        Gl.glTexCoord2f(0.0f, 0.0f);
        Gl.glVertex2f(-lenght, -height);
        Gl.glTexCoord2f(1.0f, 0.0f);
        Gl.glVertex2f(lenght, -height);
        
        Gl.glEnd();
        }

    }


}
